<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <style>
        table {
            width: 500px;
            margin: 20px auto;
            text-align: center;
            padding: 0px;
        }
        
        #foot {
            width: 500px;
            margin: 3px auto;
        }
        
        #foot div {
            width: 200px;
            float: right;
        }
        
        thead {
            background-color: cornflowerblue;
        }
        
        table,
        td,
        th {
            padding: 0px;
            border-collapse: collapse;
            padding: 5px 3px;
            height: 20px;
            line-height: 20px;
            border-bottom: 1px solid #777;
        }
        
        .bgeven {
            background-color: rgba(35, 196, 129, 0.473);
        }
        
        .bgodd {
            background-color: beige;
        }
    </style>
</head>

<body>


    <table>
        <thead>
            <tr>
                <td>
                    <input type="checkbox" name="all" id="all">
                </td>
                <td>序号</td>
                <td>姓名</td>
                <td>年龄</td>
                <td>成绩</td>
                <td>
                    操作
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" name="item" id="" value="">
                </td>

                <td>1</td>
                <td>张三</td>
                <td>20</td>
                <td>90.5</td>
                <td>
                    <a href="javascript:void(0);">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="item" id="" value="">
                </td>

                <td>2</td>
                <td>李四</td>
                <td>21</td>
                <td>89</td>
                <td>
                    <a href="javascript:void(0)">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="item" id="" value="">
                </td>

                <td>3</td>
                <td>王五</td>
                <td>20</td>
                <td>60</td>
                <td>
                    <a href="javascript:void(0);">删除</a>
                </td>
            </tr>

            <tr>
                <td>
                    <input type="checkbox" name="item" id="" value="">
                </td>

                <td>4</td>
                <td>王刘</td>
                <td>15</td>
                <td>14</td>
                <td>
                    <a href="javascript:void(0);">删除</a>
                </td>
            </tr>
        </tbody>

    </table>
    <div id="foot">
        <button>反选</button>
        <button id="del">删除选中</button>
        <div id="price">
            <span>总分</span>
            <span id="total">0.0</span>
        </div>

    </div>

</body>
<!-- javaScript写的
<script>
    var trs = document.querySelectorAll("tbody tr")
    for(var i = 0;i<trs.length;i++){
        trs[i].onmouseover = function(){
            this.style.backgroundColor = "pink"
        }
        trs[i].onmouseout = function(){
            this.style.backgroundColor = ""
        }
    }
</script> -->
<!-- jQuery写的 -->
<script>
$('tbody tr').hover(function () {
    $(this).css('backgroundColor','pink')
},function () {
    $(this).css('backgroundColor','')
    
})
$(document).ready(function () {
    const $tbody = $('tbody');
    const $total = $('#total');
    const $checkboxes = $('input[name="item"]');
    //删除功能
    $tbody.on('click','a',function () {
        $(this).closest('tr').remove();
        updateCheckboxState()
        calculateTotal()
    })

    //全选功能
    $('#all').on('change',function () {
        $checkboxes.prop('checked',this.checked)
        calculateTotal()
    })

    //删除选中
    $('#del').click(function () {
        $checkboxes.filter(':checked').closest('tr').remove();
        updateCheckboxState()
        calculateTotal()
    })

    // 总分计算功能
    function calculateTotal() {
        const total = $('input[name="item"]:checked').map(function () {
            return parseFloat($(this).closest('tr').find('td:eq(4)').text()) || 0;
        }).get().reduce((sum,value)=>sum + value,0);
        $total.text(total.toFixed(1));
        
    }

    // 单个复选框变化时更新全选框状态
        function updateCheckboxState() {
            if($('input[name="item"]:checked').length==0){
                $('#all').prop('checked',false)
                calculateTotal();
                return;
            }
            const allChecked = $('input[name="item"]').length === $('input[name="item"]:checked').length;
            $('#all').prop('checked', allChecked);
            calculateTotal();
        }

        $tbody.on('change', 'input[name="item"]', updateCheckboxState);

    // 反选功能
    $('#foot button:first').click(function () {
        $checkboxes.each(function () {
            $(this).prop('checked', !$(this).prop('checked'));
        });
        updateCheckboxState();
    })

})
</script>
</html>